/**
 * @Author: Bobby
 * @Date: 2022-02-15 13:14:17
 * @LastEditTime: 2022-03-22 09:41:41
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\ve\mixin\uiMixin.js
 * @CIMC-V2
 */
// import { eventTask } from '@ve/eventTask/index'
//主要使用在预览页面 elements Ui
export default {
  props: {
    info: {
      required: true,
      type: Object,
      default: function () {
        return {}
      },
    },
    formDataValue: {
      required: true,
    },
    formDataKey: {
      required: true,
    },
    formDataLabel: {
      required: true,
    },
    tableRef: {
      required: true,
      type: Object,
      default: function () {
        return {}
      },
    },
    disabled: {
      default: false,
    },
  },
  emits: ['formDataItemChange', 'editNextRow'],
  inject: {
    hasPreview: { default: false },
    development: { default: false },
  },
  data() {
    return {
      formDataItem: {},
    }
  },
  computed: {
    getHasPreview() {
      return this.$store.getters['preview/getHasPreview']
    },
    getInfo() {
      return this.info
    },
  },
  watch: {
    formDataItem: {
      deep: true,
      handler(newVal) {
        this.$emit('formDataItemChange', newVal)
      },
    },
    formDataValue: {
      deep: true,
      handler(newVal) {
        this.formDataItem[this.formDataKey] = newVal
      },
    },
  },
  created() {
    this.formDataItem[this.formDataKey] = this.formDataValue
  },
  // eslint-disable-next-line prettier/prettier
  mounted() { },
  methods: {
    // 聚焦下一个组件
    nextModuleFocus() {
      const moduleList = this.$store.getters['tableFormItem/getTableFormItemList']
      for (let i = 0; i < moduleList.length; i++) {
        if (moduleList[i].id === this.moduleId) {
          // 找到当前组件的位置然后聚焦下一个组件
          if (i < moduleList.length - 1) {
            // 如果当前组件为最后一个可编辑单元 则另作处理
            const nextModule = moduleList[i + 1] && moduleList[i + 1].ref
            const currentModule = moduleList[i] && moduleList[i].ref
            // 如果当前组件是下拉选择组件 则手动失焦
            if (currentModule && moduleList[i].id.indexOf('uiTableSelect') > -1) currentModule.blur()
            if (nextModule) {
              nextModule.focus()
              this.$nextTick(() => {
                nextModule.select && nextModule.select()
              })
            }
            // 如果下一个组件是下拉选择组件 则显示下拉选择框
            // if (nextModule && moduleList[i + 1].id.indexOf('uiTableSelect') > -1) nextModule.showPanel()
          } else {
            // 如果当前为最后一个可编辑单元 则跳转至下一行并清空当前编辑表单列表
            // 先触发表格编辑的校验
            // 先验证表格数据
            // 获取当前编辑的数据
            const { row } = this.tableRef.getEditRecord()
            // 校验数据
            this.tableRef.validate(row).then((valid) => {
              if (!valid) {
                this.$store.dispatch('tableFormItem/clearTableFormItemList')
                this.$emit('editNextRow')
              }
            })
          }
          break
        }
      }
    },
  },
}
